//
//  STTimerManageVC.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/12.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STTimerManageVC.h"
#import "STTimerManagerView.h"
#import "STTimerPickerView.h"
#import "STTimerDaySelView.h"
#import "UIButton+ZYHblock.h"

@interface STTimerManageVC ()<STTimerManagerViewDelegate>{
    UIView * backView;
    STTimerManagerView * timerView;
    STTimerPickerView * timerPicekerView;
    STTimerDaySelView * dayTimerSelView;
    NSMutableArray * arrTimers;
    TimerSelDayType currentDay;
}

@property(nonatomic,strong) NSDictionary * serTimerDic;
@end

@implementation STTimerManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"时间管理";
    [self wr_setNavBarTintColor:[UIColor blackColor]];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.serTimerDic = [NSMutableDictionary dictionaryWithCapacity:4];
    arrTimers = [NSMutableArray arrayWithCapacity:7];
    [self loadDefaultTimerModels:nil];
    [self loadTimerManagerData];
    [self loadTimerManagerView];
}

-(void)loadDefaultTimerModels:(NSDictionary * )dicTimer{
    if (!dicTimer) {
        dicTimer = @{@"fromHour":@"10",@"fromMinute":@"00",@"endHour":@"23",@"endMinute":@"00"};
    }
    
    if(!arrTimers.count){
        arrTimers = [NSMutableArray arrayWithCapacity:7];
        for (int i=1; i<=7;i++) {
            STHairTimerModel * model = [[STHairTimerModel alloc]init];
            model.stylistId = [[YLUserInfo sharedInstance].stylistId longValue];
            model.workday = i;
            model.status = 0;
            model.starttime = [NSString stringWithFormat:@"%@:%@:00",dicTimer[@"fromHour"],dicTimer[@"fromMinute"]];
            model.endtime =[NSString stringWithFormat:@"%@:%@:00",dicTimer[@"endHour"],dicTimer[@"endMinute"]];
            [arrTimers addObject:model];
        }
    }else{
        for (STHairTimerModel * model in arrTimers) {
            model.starttime = [NSString stringWithFormat:@"%@:%@:00",dicTimer[@"fromHour"],dicTimer[@"fromMinute"]];
            model.endtime =[NSString stringWithFormat:@"%@:%@:00",dicTimer[@"endHour"],dicTimer[@"endMinute"]];
        }
    }
    
}

- (void)loadTimerManagerData {
    [STHairService getStylistTimeSetting:^(id result, NSError *error) {
        if(error){
            [ZYHCommonMethod showError:error];
        }
        NSArray * arrNetData =[NSMutableArray arrayWithArray:[NSArray modelArrayWithClass:[STHairTimerModel class] json:result]];
        for (STHairTimerModel * model in arrNetData) {
            if(model.workday<=7 && model.workday>0){
                [arrTimers replaceObjectAtIndex:(model.workday-1) withObject:model];
            }
        }
        [dayTimerSelView setTimerDaySelView:arrTimers];
    }];
}

- (void)loadTimerManagerView {
    timerView = [[STTimerManagerView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    timerView.delegate = self;
    timerView.serviceTimerDic =@{@"fromHour":@"10",@"fromMinute":@"00",@"endHour":@"23",@"endMinute":@"00"};
    timerView.backgroundColor = [UIColor clearColor];
    [timerView setMyTimerManagerViewWithTimerData:timerView.serviceTimerDic];
    [self.view addSubview:timerView];
}

-(void)loadBackView{
    if (!backView) {
        backView  = [[UIView alloc]initWithFrame:self.view.bounds];
        backView.backgroundColor = [UIColor blackColor];
        backView.alpha = 0.5;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cleanViews)];
        [backView addGestureRecognizer:tap];
    }
    if (![self.view.subviews containsObject:backView]) {
        [self.view addSubview:backView];
    }
}

-(void)timerMagSelTimer{
    [self loadBackView];
    currentDay = ServiceTimer;
    [self loadTiemrPickerWithOldDic:nil];
    
}

-(void)loadTiemrPickerWithOldDic:(NSDictionary *)oldTimerDic{
    NSMutableArray * hours = [NSMutableArray arrayWithCapacity:24];
    NSMutableArray * minutes = [NSMutableArray arrayWithCapacity:60];
    for (int i = 0; i<24; i++) {
        NSString * hour = [NSString stringWithFormat:@"%02d",i];
        [hours addObject:hour];
    }
    for (int j = 0; j<60; j++) {
        NSString * minute = [NSString stringWithFormat:@"%02d",j];
        [minutes addObject:minute];
    }
    NSArray * arrTimerSel = @[hours,minutes,hours,minutes];
    if (!timerPicekerView) {
        WeakSelf
        timerPicekerView = [[STTimerPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [timerPicekerView setTimerPickerViewWithArr:arrTimerSel withHolder:oldTimerDic];
        __weak typeof(timerView) weakTimerView = timerView;
        __weak typeof(dayTimerSelView) weakDayTimerSelView = dayTimerSelView;
        timerPicekerView.selectedItemBlock = ^(NSArray *addressArr) {
            
            NSString *county1 = [addressArr objectAtIndex:0];
            NSString *county2 = [addressArr objectAtIndex:1];
            NSString *county3 = [addressArr objectAtIndex:2];
            NSString *county4 = [addressArr objectAtIndex:3];
            [weakSelf.serTimerDic setValue:county1 forKey:@"fromHour"];
            [weakSelf.serTimerDic setValue:county2 forKey:@"fromMinute"];
            [weakSelf.serTimerDic setValue:county3 forKey:@"endHour"];
            [weakSelf.serTimerDic setValue:county4 forKey:@"endMinute"];
            if(currentDay == 0){
                [weakTimerView setServiceDayType:currentDay WithDic:weakSelf.serTimerDic];
                [weakSelf loadDefaultTimerModels:weakSelf.serTimerDic];
                [dayTimerSelView setTimerDaySelView:arrTimers];
            }else{
                [weakDayTimerSelView changeDayTimer:weakSelf.serTimerDic day:currentDay];
                
                [weakTimerView setMyTimerManagerViewWithTimerData:nil];
            }
            
            NSLog(@"%@",weakSelf.serTimerDic);
            
        };
        timerPicekerView.cancelItemBlock = ^{
            [weakSelf cancelTimerPickerView];
        };
    }else{
        [timerPicekerView setTimerPickerViewWithArr:arrTimerSel withHolder:oldTimerDic];
    }
    
    [self.view addSubview:timerPicekerView];
}

-(void)timerMagSelAutoDayTimer{
    [self loadBackView];
    if (!dayTimerSelView) {
        dayTimerSelView = [[STTimerDaySelView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-(9*45), SCREEN_WIDTH, (9*45))];
        WeakSelf
        dayTimerSelView.selectedDayBlock = ^(int selNum,NSString * oldTimer) {
            [weakSelf loadTimerPickerViewFormDayTimer:selNum oldTimer:oldTimer];
        };
        dayTimerSelView.sureDayBlock = ^(NSArray *dayInfo) {
            [weakSelf sureDayTimerSelData:dayInfo];
        };
    }
    [dayTimerSelView setTimerDaySelView:arrTimers];
    [self.view addSubview:dayTimerSelView];
}

//从星期自定义 打开 时间picker
-(void)loadTimerPickerViewFormDayTimer:(int)selNum oldTimer:(NSString*)oldTimer{
    currentDay = selNum;
    dayTimerSelView.hidden = YES;
    NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:4];
    NSArray * arr=[oldTimer componentsSeparatedByString:@"-"];
    if(arr.count ==2){
        NSString * formStr =arr.firstObject;
        NSString * endStr =arr.lastObject;
        [dic setValue:[formStr substringToIndex:2] forKey:@"fromHour"];
        [dic setValue:[formStr substringWithRange:NSMakeRange(3, 2)] forKey:@"fromMinute"];
        [dic setValue:[endStr substringToIndex:2] forKey:@"endHour"];
        [dic setValue:[endStr substringWithRange:NSMakeRange(3, 2)] forKey:@"endMinute"];
    }
    
    [self loadTiemrPickerWithOldDic:dic];
}

//确定星期的自定义时间
-(void)sureDayTimerSelData:(NSArray *)dayInfo{
    arrTimers =[NSMutableArray arrayWithArray:dayInfo];
    [self cleanViews];
}

//服务开关
-(void)timerMagOpenTheDoor:(UISwitch *)sender{
    if(sender.on){
        [timerView setUnEnable:YES];
        return;
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"关闭营业开关后门店不再接受新的订单      !" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        sender.on = !sender.on ;
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if(!sender.on){
            [timerView setUnEnable:NO];
        }
    }];
    [alertController addAction:otherAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}



-(void)cancelTimerPickerView{
    [timerPicekerView removeFromSuperview];
    if([self.view.subviews containsObject:dayTimerSelView]){
        dayTimerSelView.hidden = NO;
    }else{
        [backView removeFromSuperview];
    }
}

-(void)cleanViews{
    [backView removeFromSuperview];
    [timerPicekerView removeFromSuperview];
    [dayTimerSelView removeFromSuperview];
}


//保存
-(void)timerMagSaveTimer:(NSDictionary *)tiemrDic{
    NSArray * arr = [NSArray arrayWithArray:[arrTimers modelToJSONObject]];
    [STHairService updateStylistTimeSetArrParatemer:arr completion:^(id result, NSError *error) {
        if(error){
            [ZYHCommonMethod showError:error];
        }else{
            [ZYHCommonMethod showSuccess:@"保存成功"];
        }
    }];
}


@end
